package cn.wolfcode.wolf2w.job;


import cn.wolfcode.wolf2w.redis.service.IStrategyRedisService;
import cn.wolfcode.wolf2w.redis.service.ITravelRedisService;
import cn.wolfcode.wolf2w.redis.vo.TravelStatisVo;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisDataPersistenceJob {

	@Autowired
	private IStrategyRedisService strategyRedisService;
	@Autowired
	private IStrategyService strategyService;
	@Autowired
	private ITravelRedisService travelRedisService;
	@Autowired
	private ITravelService travelService;
	@Autowired
	private IUserInfoService userInfoService;

	// 持久化攻略vo
	// 设置多久执行一次的时间,目前是秒
	@Scheduled(cron = "10 * * * * ? ")
	public void persistenceStrategyVo(){
		System.out.println("攻略vo开始了");
		// 所有vo
		List<StrategyStatisVo> vos = strategyRedisService.queryStrategyVoByPattern("*");
		// 遍历vo,然后持久化mongodb中的strategy
		vos.forEach(vo->strategyService.updateStrategyVo(vo));

		System.out.println("攻略vo结束了");
	}
	// 持久化用户攻略收藏列表
	@Scheduled(cron = "10 * * * * ? ")
	public void persistenceStrategyFavor(){
		System.out.println("攻略收藏开始了");
		// 所有用户的收藏列表
		List<List<String>> allFavorList = strategyRedisService.getAllFavorList();

		// 遍历列表,然后持久化mongodb中的strategy
		allFavorList.forEach(list->userInfoService.updateStrategyFavor(list));

		System.out.println("攻略收藏结束了");
	}

	// 持久化攻略vo
	@Scheduled(cron = "10 * * * * ? ")
	public void persistenceTravelStrategyVo(){
		System.out.println("攻略vo开始了");
		// 所有vo
		List<TravelStatisVo> vos = travelRedisService.queryTravelVoByPattern("*");
		// 遍历vo,然后持久化mongodb中的travel
		vos.forEach(vo->travelService.updateTravelVo(vo));

		System.out.println("攻略vo结束了");
	}
	// 持久化用户收藏游记列表
	@Scheduled(cron = "10 * * * * ? ")
	public void persistenceTravelFavor(){
		System.out.println("游记收藏开始了");
		// 所有用户的收藏列表
		List<List<String>> allFavorList = travelRedisService.getAllFavorList();

		// 遍历列表,然后持久化mongodb中的strategy
		allFavorList.forEach(list->userInfoService.updateTravelFavor(list));

		System.out.println("游记收藏结束了");
	}
}
